﻿

using Controller.i18n.entity;
using HBase;
using hCommand;
using hconnect;
using MongoDB.Bson.IO;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Controller {
    public abstract class CraneAllocateJob :AbsCraneJjlAllocate { 
      public override BaseTask analyseSingleTaskModel(CraneStatus craneStatus,      List<TaskCrane> tclist )  {
           
            carrierRemove(tclist);
            reDirect(tclist, craneStatus);
            //skipPk(tclist);
            if (tclist.Count == 0) {
                return null;
            } else {
                String info = getCNo() + "号垛机可选任务";
                foreach (var t in tclist) {  
                    info += t.taskNo+","; 
                } 
                consoleOnly(InfoC.consoleOnly_crane_Working, info);
            }

            TaskCrane ct = priorityOut(tclist);
            if (ct != null) {
                return ct;
            }
            TaskCrane taskCrane = tclist.Where(x => x.des.GetXiangDaoNo() == craneStatus.xdh
            || x.src.GetXiangDaoNo() == craneStatus.xdh || (I18nConfig.isLoc(x.src) && I18nConfig.isLoc(x.des))).FirstOrDefault();
            if (taskCrane != null) {
                return taskCrane;
            } else {
                TaskCrane taskCrane1 = MyCache.getList<TaskCrane>().Where(x => ((x.des.GetXiangDaoNo() == craneStatus.xdh
             || x.src.GetXiangDaoNo() == craneStatus.xdh)) && x.craneId == this.getCNo() && x.status == 11).FirstOrDefault();
                if (taskCrane1 != null) {
                    consoleOnly(InfoC.consoleOnly_crane_Working, taskCrane1.taskNo + "此巷道任务没有执行完" + craneStatus.xdh);
                    return null;
                }
            }
            return tclist[0];
        }
        TaskCrane priorityOut(List<TaskCrane> tclist) {
            int count = 0;
            TaskCrane taskCrane = null;
            foreach (TaskCrane ct in tclist) {
                if (ct.priority > 0) {
                    count++;
                    if (taskCrane == null || taskCrane.priority < ct.priority) {
                        taskCrane = ct;
                    } 
                } 
            }
            consoleOnly(InfoC.consoleOnly_crane_Working,   "优先可执行任务数" + count);
            return taskCrane; 
        } 
        public void skipPk(List<TaskCrane> tclist) {
            List<TaskCrane> removeList = new List<TaskCrane>();
            foreach (TaskCrane crane in tclist) {
                CoreTask ct = taskServer.getById<CoreTask>(crane.completeId);
                if (ct.bussType == 31) {
                    removeList.Add(crane);
                } 
            }

            foreach (TaskCrane ft in removeList) {
                if (tclist.Contains(ft)) {
                    tclist.Remove(ft);
                }
            }
        } 
        public void reDirect(List<TaskCrane> allFtList, CraneStatus craneStatus) {
            List<TaskCrane> removeList = new List<TaskCrane>();
            foreach (TaskCrane ft in allFtList) {
                if (I18nConfig.isLoc(ft.des)) {
                    //出库
                }
            }
            foreach (TaskCrane ft in removeList) {
                if (allFtList.Contains(ft)) {
                    allFtList.Remove(ft);
                }
            }
        }

      protected abstract   void outCarrierCheck(List<TaskCrane> removeList, TaskCrane ft);

        public bool ChangeOutTask(int stn,   TaskCrane ft) {

            CarrierSignalStatus ssOut = StatusCache.getSignalStatus<CarrierSignalStatus>(stn);

            if (ssOut.isFree()) {
                ft.changeDes(stn);
                return true;
            }
            consoleOnly(InfoC.consoleOnly_crane_Working, stn + "不是空闲" );
            return false;
        }
        public void carrierRemove(List<TaskCrane> allFtList) {
            List<TaskCrane> removeList = new List<TaskCrane>();
            BParam pp = MyCache.getValue<BParam>(getCNo()-90);
            
            foreach (TaskCrane ft in allFtList) {
                if (!I18nConfig.isLoc(ft.des)) { 
                    if (pp == null || pp.v == 0) {
                        //出库
                        outCarrierCheck(removeList, ft);
                    } else {
                        consoleOnly(InfoC.consoleOnly_crane_Working, getCNo() + "禁止出库" );
                        removeList.Add(ft);
                    }
                   
                }
                if (!I18nConfig.isLoc(ft.src)) {
                    int p = Math.Abs(ft.src);
                    int count = 0;
                    foreach (TaskCrane tc in allFtList) {
                        if (tc.src == p) {
                            count++;
                        }
                    }
                    if (count > 1) {
                        removeList.Add(ft);
                        consoleOnly(InfoC.consoleOnly_crane_Working, p + "入库任务过多");
                    }

                    CarrierSignalStatus ssIn1 = StatusCache.getSignalStatus<CarrierSignalStatus>(p);

                    if (ssIn1 == null || !ssIn1.isArrived()) {
                        consoleOnly(InfoC.consoleOnly_crane_Working, p + "异常或者，输送线不允许取料");
                        removeList.Add(ft);
                    }
                }

            }
            foreach (TaskCrane ft in removeList) {
                if (allFtList.Contains(ft)) {
                    allFtList.Remove(ft);
                }
            }
        }
    }
}
